package com.xpec.c4.service.module.common.db;

import java.util.HashMap;

import redis.clients.jedis.Jedis;

import com.ms.service.db.jedis.JedisManager;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.common.content.PlayerContent;
import com.xpec.c4.service.module.common.content.SessionContent;

public class CommonDAO {

	private final static String SESSION_CONTENT_KEY = "PlayerCommonInfo:SessionContent:%s";

	private final static String IP = "ip";

	private final static String PLATFORM = "platform";

	private final static String GATEWAY = "gateWay";
	
	private final static String BACKGROUND_MODE = "backgroundMode";

	private final static String PLAYER_CONTENT_KEY = "PlayerCommonInfo:PlayerContent:%s";

	private final static String SESSIONID = "sessionId";

	private final static String NAME = "name";

	private final static String ACCOUNTID = "accountId";

	private final static String ZONEID = "zoneId"; 
	
	private final static String PLAYERID = "playerId";

	private final static String NAME_PLAYER_KEY = "PlayerCommonInfo:NamePlayerId";

	public static void saveSessionContent(GUID sessionId, SessionContent content) {
		if (content == null) {
			return;
		}

		Jedis jedis = getJedis();

		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());

		HashMap<String, String> fieldMap = new HashMap<String, String>();
		
		fieldMap.put(IP, GameUtility.nullToEmptyString(content.getIp()));

		fieldMap.put(PLATFORM, GameUtility.nullToEmptyString(content.getPlatform()));

		fieldMap.put(GATEWAY, GameUtility.nullToEmptyString(content.getGateway()));
		
		fieldMap.put(BACKGROUND_MODE, String.valueOf(content.isBackgroundMode()));
		
		//**playerId 另外放

		jedis.hmset(key, fieldMap);

		returnJedis(jedis);
	}
	
	public static SessionContent getSessionContent(GUID sessionId){
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		HashMap<String, String> contentMap = (HashMap<String, String>) jedis.hgetAll(key);
		returnJedis(jedis);
		SessionContent content = null;
		if(contentMap != null && contentMap.size() > 0){
			content = new SessionContent();
			content.setGateway(contentMap.get(GATEWAY));
			content.setIp(contentMap.get(IP));
			content.setPlatform(contentMap.get(PLATFORM));
			content.setBackgroundMode(Boolean.valueOf(contentMap.get(BACKGROUND_MODE)));
		}
		return content;
	}

	public static void removeSessionContent(GUID sessionId) {
		Jedis jedis = getJedis();

		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());

		jedis.del(key);

		returnJedis(jedis);
	}

	/**
	 * 從Redis取得 PlayerIP
	 * 
	 * @param sessionId
	 * @return
	 */
	public static String getIPBySessionId(GUID sessionId) {
		if (sessionId == null) {
			return null;
		}
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		String value = jedis.hget(key, IP);
		returnJedis(jedis);
		return value;
	}

	/**
	 * 從Redis 取得 PlatForm
	 * 
	 * @param sessionId
	 * @return
	 */
	public static String getPlatFormBySessionId(GUID sessionId) {
		if (sessionId == null) {
			return null;
		}
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		String value = jedis.hget(key, PLATFORM);
		returnJedis(jedis);
		return value;
	}

	/**
	 * 從Redis 取得 GateWay
	 * 
	 * @param sessionId
	 * @return
	 */
	public static String getGateWayBySessionId(GUID sessionId) {
		if (sessionId == null) {
			return null;
		}
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		String value = jedis.hget(key, GATEWAY);
		returnJedis(jedis);
		return value;
	}
	
	/**
	 * 從Redis 用Session 取得 playerId
	 * @param sessionId
	 * @return
	 */
	public static GUID getPlayerIdBySessionId(GUID sessionId){
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		String value = jedis.hget(key, PLAYERID);
		returnJedis(jedis);
		return GUID.parseUUID(value);
	}
	
	/**
	 * 將 playerId 加入 SessionContent
	 * @param playerId
	 * @param sessionId
	 */
	public static void addPlayerIdToSessionContent(GUID playerId, GUID sessionId){
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, GameUtility.nullToEmptyString(sessionId.toString()));
		jedis.hset(key, PLAYERID, playerId.toString());
		returnJedis(jedis);
	}
	
	/**
	 * 用SessionId 把Player 從 SessionContent移除
	 * @param sessionId
	 */
	public static void removePlayerBySessionId(GUID sessionId){
		Jedis jedis = getJedis();
		String key = String.format(SESSION_CONTENT_KEY, sessionId.toString());
		jedis.hdel(key, PLAYERID);
		returnJedis(jedis);
	}
	
	
	
	
	/**
	 * 將playerId 有關的 存到Redis
	 * @param playerId
	 * @param content
	 */
	public static void savePlayerContent(GUID playerId, PlayerContent content) {
		if (content == null) {
			return;
		}

		Jedis jedis = getJedis();

		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());

		HashMap<String, String> fieldMap = new HashMap<String, String>();

		fieldMap.put(ACCOUNTID, GameUtility.nullToEmptyString(content.getAccountId()));

		fieldMap.put(SESSIONID, GameUtility.nullToEmptyString(content.getSessionId()));

		fieldMap.put(NAME, GameUtility.nullToEmptyString(content.getName()));

		jedis.hmset(key, fieldMap);

		returnJedis(jedis);
	}

	/**
	 * 將zoneId 存到 PlayerContent
	 * @param playerId 
	 * @param zoneId
	 */
	public static void addZoneIdToPlayerContent(GUID playerId, String zoneId) {
		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, GameUtility.nullToEmptyString(playerId.toString()));
		jedis.hset(key, ZONEID, zoneId);
		returnJedis(jedis);
	}
	
	/**
	 * 從 playerContent 移除 zoneId
	 * @param zoneId
	 * @param playerId
	 */
	public static void removeZoneIdByPlayer(String zoneId, GUID playerId){
		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());
		jedis.hdel(key,ZONEID);
		returnJedis(jedis);
	}

	
	public static void removePlayerContent(GUID playerId) {
		Jedis jedis = getJedis();

		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());

		jedis.del(key);

		returnJedis(jedis);
	}

	public static GUID getSessionByPlayerId(GUID playerId) {
		if (playerId == null) {
			return null;
		}

		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());
		String value = jedis.hget(key, SESSIONID);
		returnJedis(jedis);
		return GUID.parseUUID(value);
	}

	public static String getNameByPlayer(GUID playerId) {
		if (playerId == null) {
			return null;
		}

		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());
		String value = jedis.hget(key, NAME);
		returnJedis(jedis);
		return value;
	}

	public static GUID getAccountIdByPlayerId(GUID playerId) {
		if (playerId == null) {
			return null;
		}

		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());
		String value = jedis.hget(key, ACCOUNTID);
		returnJedis(jedis);
		return GUID.parseUUID(value);
	}

	public static String getZoneIdByPlayerId(GUID playerId) {
		if (playerId == null) {
			return null;
		}

		Jedis jedis = getJedis();
		String key = String.format(PLAYER_CONTENT_KEY, playerId.toString());
		String value = jedis.hget(key, ZONEID);
		returnJedis(jedis);
		return value;
	}

	// ----------  Name player Pair ----------------
	
	public static void addNamePlayerPair(String name, GUID playerId) {
		Jedis jedis = getJedis();
		String key = String.format(NAME_PLAYER_KEY, GameUtility.nullToEmptyString(playerId.toString()));
		jedis.hset(key, name, playerId.toString());
		returnJedis(jedis);
	}

	public static void removeNamePlayerPair(String name) {
		Jedis jedis = getJedis();
		jedis.hdel(NAME_PLAYER_KEY, name);
		returnJedis(jedis);
	}
	
	public static GUID getPlayerByName(String name) {
		Jedis jedis = getJedis();
		String value = jedis.hget(NAME_PLAYER_KEY, name);
		returnJedis(jedis);
		return GUID.parseUUID(value);
	}
	
	public static void flushRedisCommon(){
		Jedis jedis = getJedis();
		jedis.flushDB();
		returnJedis(jedis);
	}

	private static Jedis getJedis() {
		return JedisManager.getResource(ModuleName.COMMON);
	}

	private static void returnJedis(Jedis jedis) {
		JedisManager.returnResource(ModuleName.COMMON, jedis);
	}
}